#!/usr/bin/env php
<?php

/**
 * This file is part of the Twig Gettext utility.
 *
 *  (c) Саша Стаменковић <umpirsky@gmail.com>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

/**
 * Extracts translations from twig templates.
 *
 * @author Саша Стаменковић <umpirsky@gmail.com>
 */

/**
 * Adapted for ONKI Light by Osma Suominen. Changes:
 * - added dummy implementation of link_url filter used by ONKI Light
 * - changed require_once statement below so it finds autoload.php
 */

/* if (file_exists($a = __DIR__.'/../../autoload.php')) {
    require_once $a;
} else {
    require_once __DIR__.'/vendor/autoload.php';
} */
require_once __DIR__.'/../vendor/autoload.php';

$twig = new Twig_Environment(new Twig\Gettext\Loader\Filesystem('/'), array(
    'cache'       => '/tmp/cache/'.uniqid(),
    'auto_reload' => true
));
$twig->addExtension(new Symfony\Bridge\Twig\Extension\TranslationExtension(
    new Symfony\Component\Translation\Translator(null)
));
$twig->addExtension(new Twig_Extensions_Extension_I18n());
$twig->addExtension(new Symfony\Bridge\Twig\Extension\RoutingExtension(
    new Twig\Gettext\Routing\Generator\UrlGenerator()
));
$twig->addExtension(new Symfony\Bridge\Twig\Extension\FormExtension(
    new Symfony\Bridge\Twig\Form\TwigRenderer(
        new Symfony\Bridge\Twig\Form\TwigRendererEngine()
    )
));
// You can add more extensions here.

// Custom filter for ONKI Light templates
$urlFilter = new Twig_SimpleFilter('link_url',
    function($uri, $vocab, $lang,$type='page') {
        return $uri; // dummy implementation
    });
$twig->addFilter($urlFilter);

array_shift($_SERVER['argv']);
$addTemplate = false;

$extractor = new Twig\Gettext\Extractor($twig);

foreach ($_SERVER['argv'] as $arg) {
    if ('--files' == $arg) {
        $addTemplate = true;
    } else if ($addTemplate) {
        $extractor->addTemplate(getcwd().DIRECTORY_SEPARATOR.$arg);
    } else {
        $extractor->addGettextParameter($arg);
    }
}

$extractor->extract();
